package com.larry.mapreduce.writableComparable;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * @ProjectName MapReduceDemo
 * @Author larry
 * @Date 11:22 2021/11/7
 * @Description TODO
 *
 * 1.定义类实现writable接口
 * 2.重写序列化和反序列化接口
 * 3.重写空参构造
 * 4，重写tostring()
 **/
public class FlowBean implements WritableComparable<FlowBean> {

    private long upFlow;
    private long downFlow;
    private long sunFlow;

    public long getUpFlow() {
        return upFlow;
    }

    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }

    public long getDownFlow() {
        return downFlow;
    }

    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }

    public long getSunFlow() {
        return sunFlow;
    }

    public void setSunFlow() {
        this.sunFlow = this.upFlow + this.downFlow;
    }

    public FlowBean() {
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(upFlow);
        dataOutput.writeLong(downFlow);
        dataOutput.writeLong(sunFlow);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.upFlow = dataInput.readLong();
        this.downFlow = dataInput.readLong();
        this.sunFlow = dataInput.readLong();
    }

    @Override
    public String toString() {
        return upFlow +
                "\t" + downFlow +
                "\t" + sunFlow;
    }

    @Override
    public int compareTo(FlowBean o) {
        //按照总流量倒叙进行排序
        if (this.sunFlow > o.sunFlow){
            return -1;
        }else if (this.sunFlow < o.sunFlow){
            return 1;
        }else {
            //二次排序
            if (this.upFlow > o.upFlow){
                return 1;
            }
            return 0;
        }
    }
}
